package com.cloud2eye.auth.service;

import com.cloud2eye.auth.jwt.JwtUser;
import com.cloud2eye.sys.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Autowired
    PasswordEncoder passwordEncoder;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        log.info(String.format("Load APIUser by username '%s'.", username));

        JwtUser user = create(username);
        if (user == null) {
            throw new UsernameNotFoundException(String.format("No APIUser found with username '%s'.", username));
        } else {
            return create(username);
        }
    }

    @Override
    public JwtUser create(String username) {
   /*     SysUser user = sysUserMapper.queryOneByUserName(username);
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
        List<SysResource> resourceHolder=new ArrayList<SysResource>();
        Employee employee = employeeMapper.selectByPrimaryKey(user.getEmpId());


        if (user != null) {
            return new JwtUser(user.getUserId(), user.getUserName(), user.getPassword(), list, true,user.getEmpId(),employee.getEmpName());
        } else {
            return null;
        }*/
        return null;
    }

    @Override
    public void updateToken(String username,String token) {
   /*     SysUser user = sysUserMapper.queryOneByUserName(username);
        user.setToken(token);
        user.setTokenUpdateTime(new Date());
        sysUserMapper.updateByPrimaryKeySelective(user);*/
    }

    @Override
    public boolean isSameToken(String username,String token) {
        /*SysUser user = sysUserMapper.queryOneByUserName(username);
        if(user.getToken()==null){
            return true;
        }
        if(user.getToken().equals(token)){
            return true;
        }
        return false;*/
        return true;
    }

    @Override
    public boolean isEnable(String username) {
      /*  SysUser user = sysUserMapper.queryOneByUserName(username);
        if(user.getStatus().intValue()==0){//启用
            return true;
        }else {
            return false;
        }*/
      return true;
    }



  /*  @Override
    public void addAuthority(Authority authority) throws Exception {
        if (authority == null) {
            throw new Exception("Invalid input authority");
        } else {
            log.info(String.format("Add Authority '%s'.", authority.getAuthority()));
            authorityRepository.save(authority);
        }
    }*/

}
